package segimasat.gui;


import segimasat.partition.Partition;
import segimasat.partition.Listener;
import java.util.Date;
import javax.swing.SpinnerNumberModel;
import segimasat.image.Image;
import segimasat.image.floating.Gray32;
import segimasat.image.floating.StackGray32;
import segimasat.partition.Settings;



public class MainFrame extends javax.swing.JFrame {
    Image img;

    public MainFrame(Image img){
        this.img = img;
        initComponents();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jSpinner1 = new javax.swing.JSpinner();
        jSpinner2 = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        jSpinner3 = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setFont(new java.awt.Font("Calibri", 0, 10)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        jLabel1.setText("Local range parameter:");

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        jLabel2.setText("Global range parameter:");

        jButton1.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N
        jButton1.setText("Generate partition");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N
        jButton2.setText("Set default values");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jSpinner1.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        jSpinner1.setModel(new SpinnerNumberModel( Settings.DEFAULT_ALPHA,   // initial
            Settings.MIN_ALPHA,       // minimum
            Settings.MAX_ALPHA,       // maximum
            1));                     // step);
jSpinner1.setInheritsPopupMenu(true);

jSpinner2.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
jSpinner2.setModel(new SpinnerNumberModel( Settings.DEFAULT_OMEGA,   // initial
    Settings.MIN_OMEGA,       // minimum
    Settings.MAX_OMEGA,       // maximum
    1));                     // step);
    jSpinner2.setInheritsPopupMenu(true);

    jLabel3.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
    jLabel3.setText("Connectivity index:");

    jSpinner3.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
    jSpinner3.setModel(new SpinnerNumberModel( Settings.DEFAULT_CI,   // initial
        0.0,       // minimum
        1.0,       // maximum
        0.1));                     // step);
jSpinner3.setInheritsPopupMenu(true);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(83, 83, 83)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        .addGroup(layout.createSequentialGroup()
            .addGap(33, 33, 33)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel2)
                .addComponent(jLabel3)
                .addComponent(jLabel1))
            .addGap(16, 16, 16)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(jSpinner3)
                .addComponent(jSpinner2, javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jSpinner1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))))
    .addContainerGap(35, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(43, 43, 43)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(jSpinner3))
            .addGap(63, 63, 63)
            .addComponent(jButton2)
            .addGap(32, 32, 32)
            .addComponent(jButton1)
            .addGap(52, 52, 52))
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Thread t = new Thread(new Runnable(){
            public void run(){
                int alpha = (Integer)jSpinner1.getValue();
                int omega = (Integer)jSpinner2.getValue();
                double ci = (Double)jSpinner3.getValue();

                Partition p = null;
                if (img instanceof Gray32 || img instanceof StackGray32){
                    p = new segimasat.partition.floating.Partition32(img, alpha, omega, ci);
                }
                else{
                    p = new segimasat.partition.integer.Partition8(img, alpha, omega, ci);
                }

                PartitionInfo frame = new PartitionInfo(p);
                Listener lp = new Listener(p, frame);
                p.out.show();
                long time = (new Date()).getTime();
                p.makeRegions();
                time = (new Date()).getTime() - time;
                frame.setVisible(true);
                p.out.imc.addMouseListener(lp);
                p.out.imc.addMouseMotionListener(lp);
                p.out.win.addWindowListener(lp);
                frame.text_pixels.setText("" + p.out.impr.getWidth() * p.out.impr.getHeight());
                frame.text_time.setText("" + time);
                frame.text_components.setText("" + p.components.size());
            }
        });
        t.start();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        jSpinner1.setValue(Settings.DEFAULT_ALPHA);
        jSpinner2.setValue(Settings.DEFAULT_OMEGA);
        jSpinner3.setValue(Settings.DEFAULT_CI);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    private javax.swing.JSpinner jSpinner3;
    // End of variables declaration//GEN-END:variables
}
